package org.indp.mongowl.ui.container;

import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import org.bson.types.ObjectId;

import java.util.Arrays;
import java.util.Collection;

/**
 *
 */
public class MongoCursorContainer implements Container, Container.Ordered {

    public static final String OBJECT = "OBJECT";
    public static final String ID = "ID";
    public static final Collection<String> PROPERTY_IDS = Arrays.asList(ID, OBJECT);
    private final DBCursor cursor;

    public MongoCursorContainer(DBCursor cursor) {
        this.cursor = cursor;
    }

    @Override
    public Item getItem(Object itemId) {
        DBObject object = cursor.getCollection().findOne(itemId);
        return new DBObjectItem(object);
    }

    @Override
    public Collection<?> getContainerPropertyIds() {
        return PROPERTY_IDS;
    }

    @Override
    public Collection<?> getItemIds() {
        throw new UnsupportedOperationException();
        //return new ItemIdsCollection(cursor.copy());
    }

    @Override
    public Property getContainerProperty(Object itemId, Object propertyId) {
        if (itemId instanceof ObjectId) {
            return new DBObjectItem((DBObject) itemId).getItemProperty(propertyId);
        }
        throw new IllegalArgumentException();
    }

    @Override
    public Class<?> getType(Object propertyId) {
        switch (propertyId.toString()) {
            case ID:
                return ObjectId.class;
            case OBJECT:
                return DBObject.class;
            default:
                throw new IllegalArgumentException();
        }
    }

    @Override
    public int size() {
        return cursor.count();
    }

    @Override
    public boolean containsId(Object itemId) {
        // todo
        return true;
    }

    @Override
    public Item addItem(Object itemId) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    @Override
    public Object addItem() throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean removeItem(Object itemId) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean removeContainerProperty(Object propertyId) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean removeAllItems() throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    @Override
    public Object nextItemId(Object itemId) {
        //todo
        return null;
    }

    @Override
    public Object prevItemId(Object itemId) {
        //todo
        return null;
    }

    @Override
    public Object firstItemId() {
        return null;
    }

    @Override
    public Object lastItemId() {
        return null;
    }

    @Override
    public boolean isFirstId(Object itemId) {
        return false;
    }

    @Override
    public boolean isLastId(Object itemId) {
        return false;
    }

    @Override
    public Object addItemAfter(Object previousItemId) throws UnsupportedOperationException {
        return null;
    }

    @Override
    public Item addItemAfter(Object previousItemId, Object newItemId) throws UnsupportedOperationException {
        return null;
    }
}
